<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/27 0027
 * Time: 17:11
 */

namespace app\general\controller;


use Qiniu\Auth;
use think\Controller;
use think\Env;

require VENDOR_PATH.'autoload.php';
class Upload extends Controller
{
    /**
     * @OA\Get(
     *     path="/upload/qnUploadImage",
     *     tags={"general"},
     *     summary="general七牛图片上传",
     *     @OA\Response(
     *         response=405,
     *         description="Invalid input"
     *     ),
     *     security={
     *         {"petstore_auth": {"write:pets", "read:pets"}}
     *     },
     *     requestBody={"ref": "#/components/requestBodies/Pet"}
     * )
     */
    // 七牛图片上传
    public function qnUploadImage()
    {
        $auth = new Auth(config('qi_niu')['accessKey'],config('qi_niu')['secretKey']);

        //生成上传token
        $info['upload_token'] = $auth->uploadToken(config('qi_niu')['bucket']);
        $info['image_url'] = config('qi_niu')['domain_name'];
        $info['image_name'] = date('Ymd').uniqid();
        return success($info);
    }

    //上传到服务器
    public function uploadImageToServer()
    {
        // 获取表单上传文件
        $file = request()->file('image');
        if(empty($file)) error('请上传图片');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>5120000])->move(ROOT_PATH . 'public' . DS . 'upload');
        if($info){
            // 成功上传后 获取上传信息
            $filename = Env::get('domain.urls') . DS . 'upload'. DS .$info->getSaveName();
            return success($filename);
        }else{
            // 上传失败获取错误信息
            error($file->getError());
        }
    }

    //上传到服务器
    public function uploadImageToServer2()
    {
        // 获取表单上传文件
        $file = request()->file('image');
        if(empty($file)) error('请上传图片');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>5120000])->move(ROOT_PATH . 'public' . DS . 'upload');
        if($info){
            // 成功上传后 获取上传信息
            $filename = Env::get('domain.urls') . DS . 'upload'. DS .$info->getSaveName();
            $data=array(
                "status"=>1,
                "url"=>$filename
            );
           // return success($filename);
            return json($data);
        }else{
            // 上传失败获取错误信息
            error($file->getError());
            $data=array(
                "status"=>0,
                "msg"=>$file->getError()
            );
            // return success($filename);
            return json($data);
        }
    }



}


